package com.example.pufang.controller.front;

import com.example.common.CommonResult;
import com.example.pufang.pojo.User;
import com.example.pufang.service.SystemMessageService;
import com.example.pufang.service.UserService;
import com.example.pufang.vo.ChatVo;
import com.example.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/front/systemMessage")
@CrossOrigin
@Api(tags = "h5系统消息接口")
public class SystemMessageFrontController {

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private UserService userService;

    @Autowired
    private SystemMessageService systemMessageService;

    @Operation(summary = "获取最新系统消息")
    @GetMapping("/getNewMessage")
    public CommonResult<List<ChatVo>> getNewMessage(HttpServletRequest request) {
        User receiver = userService.getUserByPhone(tokenUtil.getFrontUserPhone(request));

        return CommonResult.success(systemMessageService.getFrontNewMessage(receiver));
    }


    @Operation(summary = "获取系统消息列表")
    @GetMapping("/getMessageList")
    public CommonResult<List<ChatVo>> getMessageList(HttpServletRequest request) {
        User receiver = userService.getUserByPhone(tokenUtil.getFrontUserPhone(request));

        return CommonResult.success(systemMessageService.getFrontMessageList(receiver));
    }

}
